/**
 * jims
 */
package com.jims.master.modules.blood.web;

import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.blood.entity.BloodTest;
import com.jims.master.modules.blood.service.BloodTestService;

import java.util.Date;
import java.util.List;

/**
 * 验血表Controller
 * @author CTQ
 * @version 2017-04-11
 */
@Controller
@RequestMapping(value = "${adminPath}/blood/bloodTest")
public class BloodTestController extends BaseController {

	@Autowired
	private BloodTestService bloodTestService;
	
	@ModelAttribute
	public BloodTest get(@RequestParam(required=false) String id) {
		BloodTest entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bloodTestService.get(id);
		}
		if (entity == null){
			entity = new BloodTest();
		}
		return entity;
	}
	

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(BloodTest bloodTest, Model model) {
		String testNo = bloodTestService.findMaxTestNo(bloodTest);
		bloodTest.setTestNo(String.valueOf(Integer.valueOf(testNo)+1));
		bloodTest.setRq(new Date());
		model.addAttribute("bloodTest", bloodTest);
		return "modules/blood/bloodTestForm";
	}

	/**
	 * @param bloodTest 传递参数
	 * @return String    返回类型
	* @throws
	 * @Title: findByVisitNo
	 * @Desciption: (根据住院号查询病人信息)
	 * @author CTQ
	 * @date 2017-04-12
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "findByVisitNo")
	public String findByVisitNo(BloodTest bloodTest, Model model) {
		bloodTest.updateOrgId();
		bloodTest = bloodTestService.findTestDataByParams(bloodTest);
		String testNo = bloodTestService.findMaxTestNo(bloodTest);
		bloodTest.setTestNo(String.valueOf(Integer.valueOf(testNo)+1));
		bloodTest.setRq(new Date());
		List<User> users= UserUtils.getRoleUserList(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
		model.addAttribute("users",users);
		model.addAttribute("bloodTest", bloodTest);
		return "modules/blood/bloodTestForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(BloodTest bloodTest) {
	    StringData data=new StringData();
		int i=bloodTestService.saveTest(bloodTest);
        if(i>0){
            data.setCode("success");
            data.setData("保存验血表成功");
        }else{
            data.setCode("error");
            data.setData("保存验血表失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(BloodTest bloodTest) {
	    StringData data=new StringData();
		int i=bloodTestService.delete(bloodTest);
        if(i>0){
            data.setCode("success");
            data.setData("删除验血表成功");
        }else{
            data.setCode("error");
            data.setData("删除验血表失败");
        }
		return data;
	}

}